package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.bean.User;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;
import com.selfdriving.wl.activity.NearbyFriend;

public class TravelTogetherActivity extends BaseFragmentActivity implements
		OnClickListener, OnRefreshListener, OnCancelListener
{

	private List<View> listViews; // Tab页面列表
	private ViewPager mPager;// 页卡内容
	private ImageView cursor;// 动画图片
	private Button btn1, btn2, btn3;// 页卡头标
	private int offset = 0;// 动画图片偏移量
	private int currIndex = 0;// 当前页卡编号
	private int bmpW;// 动画图片宽度
	NearbyFriend nearbyFriend;
	StartTravel travel;
	MyNearbyFriend myNearbyFriend;
	List<User> users = new ArrayList<User>();
	private int flag = 0;
	private TextView title;
	private Button seach;
	private Button rightButton;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.w_activity_travel_page);
		Utils.checkLoading(mApplication, this);
		initViews();
		initEvents();
		InitImageView();
		InitViewPager();
	}

	@Override
	protected void initViews()
	{
		btn1 = (Button) findViewById(R.id.travel_but1);
		btn2 = (Button) findViewById(R.id.travel_but2);
		btn3 = (Button) findViewById(R.id.travel_but3);
		title = (TextView) findViewById(R.id.top_text);
		title.setText("结伴游");
		seach = (Button) findViewById(R.id.top_right_seach);
		seach.setVisibility(View.VISIBLE);
		rightButton = (Button) findViewById(R.id.top_right_but);
		rightButton.setText("发布");
	}

	@Override
	protected void initEvents()
	{
		btn1.setOnClickListener(new MyOnClickListener(0));
		btn2.setOnClickListener(new MyOnClickListener(1));
		btn3.setOnClickListener(new MyOnClickListener(2));
	}

	/** 初始化动画 **/
	private void InitImageView()
	{
		cursor = (ImageView) findViewById(R.id.cursor);
		offset = (mScreenWidth / 3 - bmpW) / 2;// 计算偏移量
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);// 设置动画初始位置
	}

	/** 初始化ViewPager **/
	private void InitViewPager()
	{
		mPager = (ViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList<View>();
		getUserList();
		nearbyFriend = new NearbyFriend(mApplication, this, seach);
		travel = new StartTravel(mApplication, this, rightButton);
		myNearbyFriend = new MyNearbyFriend(mApplication, this, seach);
		listViews.add(nearbyFriend.getView());
		listViews.add(travel.getView());
		listViews.add(myNearbyFriend.getView());
		mPager.setAdapter(new MyPagerAdapter(listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	private void getUserList()
	{
		for (int i = 0; i < 7; i++)
		{
			User user = new User();
			users.add(user);
		}
	}

	/**
	 * ViewPager适配器
	 */
	public class MyPagerAdapter extends PagerAdapter
	{
		public List<View> mListViews;
		public int mCount;

		public MyPagerAdapter(List<View> mListViews)
		{
			this.mListViews = mListViews;
			mCount = mListViews.size();
		}

		public void destroyItem(View collection, int position, Object arg2)
		{
			((ViewPager) collection).removeView(mListViews.get(position));
			if (position >= mListViews.size())
			{
				int newPosition = position % mListViews.size();
				position = newPosition;
			}
			if (position < 0)
			{
				position = -position;
			}
		}

		public void finishUpdate(View arg0)
		{
		}

		public int getCount()
		{
			// return mListViews.size();
			return mCount;// 更改实现无限循环
		}

		public Object instantiateItem(View collection, int position)
		{
			try
			{
				((ViewPager) collection).addView(mListViews.get(position), 0);
			} catch (Exception e)
			{
			}
			return mListViews.get(position);
		}

		public boolean isViewFromObject(View arg0, Object arg1)
		{
			return arg0 == (arg1);
		}

		public void restoreState(Parcelable arg0, ClassLoader arg1)
		{
		}

		public Parcelable saveState()
		{
			return null;
		}

		public void startUpdate(View arg0)
		{
		}
	}

	/**
	 * 头标点击监听
	 */
	public class MyOnClickListener implements View.OnClickListener
	{
		private int index = 0;

		public MyOnClickListener(int i)
		{
			index = i;
		}

		public void onClick(View v)
		{
			mPager.setCurrentItem(index);
		}
	};

	/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener
	{

		int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
		int two = one * 2;// 页卡1 -> 页卡3 偏移量

		public void onPageSelected(int arg0)
		{
			Animation animation = null;
			switch (arg0)
			{
			case 0:
				flag = 0;
				btn1.setTextColor(getResources().getColor(R.color.white));
				btn2.setTextColor(getResources().getColor(R.color.title));
				btn3.setTextColor(getResources().getColor(R.color.title));
				if (currIndex == 1)
				{
					animation = new TranslateAnimation(one, 0, 0, 0);
				} else if (currIndex == 2)
				{
					animation = new TranslateAnimation(two, 0, 0, 0);
				}
				myNearbyFriend.togetherList.clear();
				nearbyFriend.page = 1;
				nearbyFriend
						.getTogetherList("44", null, null, null, null, null);
				seach.setVisibility(View.VISIBLE);
				rightButton.setVisibility(View.GONE);
				break;
			case 1:
				flag = 1;
				btn1.setTextColor(getResources().getColor(R.color.title));
				btn2.setTextColor(getResources().getColor(R.color.white));
				btn3.setTextColor(getResources().getColor(R.color.title));
				if (currIndex == 0)
				{
					animation = new TranslateAnimation(offset, one, 0, 0);
				} else if (currIndex == 2)
				{
					animation = new TranslateAnimation(two, one, 0, 0);
				}
				nearbyFriend.togetherList.clear();
				myNearbyFriend.togetherList.clear();
				seach.setVisibility(View.GONE);
				rightButton.setVisibility(View.VISIBLE);
				break;
			case 2:
				flag = 2;
				btn1.setTextColor(getResources().getColor(R.color.title));
				btn2.setTextColor(getResources().getColor(R.color.title));
				btn3.setTextColor(getResources().getColor(R.color.white));
				if (currIndex == 0)
				{
					animation = new TranslateAnimation(offset, two, 0, 0);
				} else if (currIndex == 1)
				{
					animation = new TranslateAnimation(one, two, 0, 0);
				}
				nearbyFriend.togetherList.clear();
				myNearbyFriend.page = 1;
				myNearbyFriend.getTogetherList();
				seach.setVisibility(View.GONE);
				rightButton.setVisibility(View.GONE);
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);// True:图片停在动画结束位置
			animation.setDuration(300);
			cursor.startAnimation(animation);
		}

		public void onPageScrolled(int arg0, float arg1, int arg2)
		{
		}

		public void onPageScrollStateChanged(int arg0)
		{
		}
	}

	public void initAnim()
	{
		if (flag == 0)
		{
			btn1.setTextColor(getResources().getColor(R.color.blue));
			btn2.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
			btn3.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
		} else if (flag == 1)
		{
			btn1.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
			btn2.setTextColor(getResources().getColor(R.color.blue));
			btn3.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
		} else
		{
			btn1.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
			btn2.setTextColor(getResources().getColor(
					R.color.wonderfui_activity_name));
			btn3.setTextColor(getResources().getColor(R.color.blue));
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.ivImg1:
			gotoImage();
			break;
		}

	}

	public void gotoImage()
	{
		if (!isLogin())
		{
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivity(intent);
		} else
		{
			Intent intent = new Intent(this, FeatureDetailsActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public void onCancel()
	{

	}

	@Override
	public void onRefresh()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub

	}

}